#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from django.shortcuts import render;
from django.http import HttpResponse, JsonResponse;
from django.db.models import Q;
import json;
from common.page_vo import *;
from app_spider.models import *;
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage

'''
Spider views
'''
__author__ = "HymanHu";

# Create your views here.

# 127.0.0.1/university/gkschool1000 ---- delete
def university(request, id):
    if request.method == "DELETE":
        print(id);
        University.objects.filter(id=id).delete();
        return JsonResponse(Result(200, "Delete success.").__dict__, safe=False);
    else:
        return JsonResponse(Result(500, "Unsupport request type %s" % (request.method)).__dict__, safe=False);

# 127.0.0.1/universities --- post {"currentPage":1, "pageSize":5, "keyword":"中国", "orderBy":"id", "direction":"desc"}
def universities(request):
    if request.method == "POST":
        # 从 request 中获取 search 对象，初始化 search 对象，反向排序，在排序字段前面添加 -
        search_deict = json.loads(request.body);
        search = Search(current_page=search_deict.get("currentPage"),
                        page_size=search_deict.get("pageSize"),
                        keyword=search_deict.get("keyword"),
                        order_by=search_deict.get("orderBy"),
                        direction=search_deict.get("direction"),
                        );
        search.init_search();

        # 根据 keyword && order by 查询所有的对象
        # 多个字段模糊查询，Q(字段名1__icontains=keyword) | Q(字段名2__icontains=keyword)
        # icontains 或 contains，区别是是否大小写敏感，竖线是或的意思
        universities = [];
        if search.keyword:
            universities = University.objects.filter(Q(school_name__icontains=search.keyword) |
                                                     Q(address__icontains=search.keyword)).order_by(search.order_by);
        else:
            universities = University.objects.order_by(search.order_by);
        total = len(universities);

        # 初始化 Paginator 对象，用 Paginator 对象进行分页
        paginator = Paginator(universities, search.page_size);
        try:
            universities = paginator.page(search.current_page);
        except EmptyPage:
            search.current_page = paginator.num_pages;
            universities = paginator.page(paginator.num_pages);

        # 包装自定义的 page 对象，返回数据
        universities_json = list(university.university_dict() for university in universities);
        return JsonResponse(
            Page(search.current_page, search.page_size, total, universities_json).__dict__, safe=False);
    else:
        return JsonResponse(Result(500, "Unsupport request type %s" % (request.method)).__dict__, safe=False);

# 127.0.0.1/coronavirusList ---- get
def coronavirusList(request):
    if request.method == "GET":
        coronavirusList = Coronavirus.objects.order_by("-date").all()[0:6];
        coronavirus_json = list(coronavirus.coronavirus_dict() for coronavirus in coronavirusList);
        return JsonResponse(coronavirus_json, safe=False);
    else:
        return JsonResponse(Result(500, "Unsupport request type %s" % (request.method)).__dict__, safe=False);
